### Copyright (C) 2010 Desmond Carter
###
### This file is part of Ar.
###
### Ar is free software: you can redistribute it and/or modify it under the
### terms of the GNU General Public License as published by the Free Software
### Foundation, either version 3 of the License, or (at your option) any later
### version.
###
### Ar is distributed in the hope that it will be useful, but WITHOUT ANY
### WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
### FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
### details.
###
### You should have received a copy of the GNU General Public License along
### with Ar. If not, see <http://www.gnu.org/licenses/>.

## This file is for functions and possibly classes that handle input to the
## program and possibly some output.

from locals import *
from pygame.locals import *
import pygame, drawtools, ar


def key_to_string(event, numbers=False):
    '''Convert from pygame key constants to the values of those keys.
    If the flag numbers is set, number keys are instead converted to integers.
    '''

    if numbers:
        try:
            return int(event.unicode)
        except ValueError:
            print "Encountered bad key in key_to_string (numbers): not integer"
            print "Returning string and ignoring"
            return event.unicode

    return event.unicode



# This desperately needs to be rewritten and generalized. It is the next thing
# on my queue (after the rendering code).
def handle_input(screen=ar.screen):
    '''Generic, hard-coded input handler.'''

    for event in pygame.event.get():

        if event.type == KEYDOWN:

            if K_0 <= event.key <= K_9: # i.e. pressed a number
                # Toggle display of that dimension.
                # Only works for single-digits right now, but should be easy to
                # fix.
                # A more pressing problem is that toggling a single displayed
                # dimension changes the layout of the board. This version of
                # the code contains an attempted fix.
                toggled = key_to_string(event, True) - 1
                try:
                    ar.drawn_dimensions.remove(toggled)
                except ValueError: # Not in list
                    ar.drawn_dimensions.append(toggled) # so we add instead
                    if len(ar.drawn_dimensions) >= 4:
                        ar.drawn_dimensions[:1] = []

                drawtools.doboth(drawtools.clear, screen=screen)
                drawtools.doboth(ar.board.draw,
                                 drawn_dimensions=ar.drawn_dimensions,
                                 screen=ar.screen)

            elif event.key == K_LEFT:
                drawtools.doboth(drawtools.scroll, dx=-10, screen=screen)
            elif event.key == K_RIGHT:
                drawtools.doboth(drawtools.scroll, dx=10, screen=screen)
            elif event.key == K_UP:
                drawtools.doboth(drawtools.scroll, dy=10, screen=screen)
            elif event.key == K_DOWN:
                drawtools.doboth(drawtools.scroll, dy=-10, screen=screen)

            # For debugging, in case you wondered
            elif event.key == K_SPACE:
                print ar.drawn_dimensions

            elif event.key == K_ESCAPE:
                pygame.quit()
                return False

        elif event.type == QUIT:
            pygame.quit()
            return False

    return True
